⛑️🔕🤺如果你願意,一層一層的設定安全性,你會發現,這篇介紹3個重要的安全性層面,Image Security、Security Context和Network Policies,負責不同的事物,在安全議題上,共同抵擋或是減輕危險行為造成的傷害
確保容器使用的image是安全的,從可信任的地方(registry),拉取經過弱點掃描,經過驗證的image,複習一下建立container時,需要使用image,其名稱完整格式為:
image: <registry>/<repository>/<image>:<tag>
docker.io
latest
設定由私有的(private)registry拉取時,需要些機密資訊
docker login private-registry.io
docker run private-registry.io/apps/internal-app
docker-registry
,名稱以regcred簡寫 (registry credential)kubectl create secret docker-registry regcred \
--docker-server=<your-registry-server> \
--docker-username=<your-username> \
--docker-password=<your-password> \
--docker-email=<your-email>
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: private-registry.io/apps/internal-app
imagePullSecrets: # 拉取image時,使用此secret
- name: regcred
定義權限控管Pod或者是container,針對程式執行可有的權限管控
定義的項目像是:
建立security context
apiVersion: v1
kind: Pod
metadata:
name: web-pod
spec:
# pod level
securityContext:
runAsUser: 1000 # 以1000這個user執行
containers:
- name: ubuntu
image: ubuntu
command: ["sleep", "3600"]
# container level
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000 # 指定掛載volume檔案目錄的群組所有權
# 只能在container level使用
capabilities:
add: ["MAC_ADMIN"]
K8s內的pod,在任何的網路配置下,都應該要是無額外配置就能互連,也就是預設叢集內pod跟service都是互相連通,這些進出的流量,設定由network policies管制 (非所有Solution皆支援,有支援的像是Kube router, Calico, Romana, Weave net等)
traffic的類型
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-policy
spec:
# 1. 抓取pod
podSelector:
matchLabels:
role: db
# 2. 指定類別
policyTypes:
- Ingress
- Egress
# 3. 分別設定規則
ingress:
- from:
- podSelector:
matchLabels:
name: api-pod
namespaceSelector:
matchLabels:
name: prod
- ipBlock: # 可接受的來源端
cidr: 192.168.1.02/32
egress:
- to:
- ipBlock:
cidr: 192.168.3.10/32
ports:
- protocol: TCP
port: 3306
networkpolicy操作指令
# 取得資訊
kubectl get networkpolies
kubectl get netpol
kubectl describe netpol <name>
# 編輯netpol
kubectl edit networkpolicy <name>
# 刪除netpol
kubectl delete networkpolicy <name>